package com.weipai.demo.service.impl;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.DRecommendMapper;
import com.weipai.demo.mapper.base.RecommendMapper;
import com.weipai.demo.model.Recommend;
import com.weipai.demo.service.RecommendShowService;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/17 18:45
 * @Version 1.0
 */
@Service
public class RecommendShowServiceImpl implements RecommendShowService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RecommendMapper recommendMapper;
    @Autowired
    private DRecommendMapper dRecommendMapper;
    //展示APP介绍的接口
    @Override
    public Response show() {

        Response response= null;
        try {
            response = new Response();
            Integer i=1;
            Recommend recommend=new Recommend();
            recommend.setId(i);
            Recommend recommend1 = recommendMapper.selectOne(recommend);
            response.put("recommends",recommend1);
            response.setCode(RetCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("show"+e.getMessage());
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        return response;
    }

    //==================================================================================

    //填写APP介绍接口
    @Override
    public Response write(String recommend,Integer id) {
        Response response=new Response();
        try {
            Recommend recommend1=new Recommend();
            recommend1.setRecommend(recommend);
            recommend1.setId(id);
            Recommend key = recommendMapper.selectByPrimaryKey(id);
            List<Recommend> recommends = recommendMapper.select(recommend1);
            if (key!=null||!key.equals("")){

               recommendMapper.updateByPrimaryKey(recommend1);
               // dRecommendMapper.updateByPrimaryKey(recommend,id);
                response.setCode(RetCode.UPDATESUCCESS);
            }else {
                recommend1.setRecommend(recommend);
                recommend1.setExhibition(2);
                recommend1.setId(1);
                int insert = recommendMapper.insert(recommend1);
                response.setCode(RetCode.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        return response;
    }
}
